package top.sdwu_top.kbase.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;
import top.sdwu_top.kbase.common.annocation.CurrentUser;
import top.sdwu_top.kbase.common.model.Wrapper;
import top.sdwu_top.kbase.dto.KbaseFileDto;
import top.sdwu_top.kbase.entity.KbaseAuthUser;
import top.sdwu_top.kbase.entity.KbaseFile;
import top.sdwu_top.kbase.entity.KbaseFileVersion;
import top.sdwu_top.kbase.entity.KbaseFileVersionApply;
import top.sdwu_top.kbase.entity.elastic.EsFile;
import top.sdwu_top.kbase.service.IEsFileService;
import top.sdwu_top.kbase.service.IKbaseFileService;
import top.sdwu_top.kbase.util.file.FileUtil;
import top.sdwu_top.kbase.vo.KbaseFileVo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 文件表 前端控制器
 * </p>
 *
 * @author zhipeng.duan@nvr-china.com
 * @since 2020-02-20
 */
@RestController
@RequestMapping("api/kbase-file")
public class KbaseFileController {

    @Autowired
    IEsFileService esFileService;


    @Autowired
    IKbaseFileService fileService;

    @PostMapping
    public Wrapper<KbaseFile> upload(@CurrentUser KbaseAuthUser user,  MultipartFile file, String fileName, String visibleType, String tagList) {
        return Wrapper.ok(fileService.upload(user, file, fileName, visibleType, tagList));
    }

    @GetMapping("{id}")
    @ApiOperation(value = "下载信息", httpMethod = "GET", notes = "下载",produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void download(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") String id, @RequestParam(value = "version", required = false) Integer version, @RequestParam(value = "applyId", required = false) String applyId) {
        fileService.download(request, response, id, version, applyId);
    }

    @PostMapping("update")
    public Wrapper<KbaseFileVersionApply> updateFile(@CurrentUser KbaseAuthUser user, MultipartFile file, String fileId, String content) {
        return Wrapper.ok(fileService.updateFile(user, fileId, file, content));
    }

    @PostMapping("page")
    public Wrapper<Page<KbaseFile>> page(@CurrentUser KbaseAuthUser user, @RequestBody KbaseFileVo vo) {
        return Wrapper.ok(fileService.page(user, vo));
    }

    @GetMapping("/detail/{id}")
    public Wrapper<KbaseFileDto> getById(@CurrentUser KbaseAuthUser user, @PathVariable("id") String id, @RequestParam(value = "version", required = false) Integer version) {
        return Wrapper.ok(fileService.getFileInfo(id, version));
    }

    @GetMapping("current/page")
    public Wrapper<Page<KbaseFile>> getCurrentUserPage(@CurrentUser KbaseAuthUser user, @RequestParam Long current, @RequestParam Long size) {
        return Wrapper.ok(fileService.currentUserPage(user, current, size));
    }

    @GetMapping("list")
    public Wrapper<List<KbaseFile>> list(@CurrentUser KbaseAuthUser user,
                                      @RequestParam(value = "keyword", required = false) String keyword) {
        return Wrapper.data(fileService.list(user, keyword));
    }


}

